package hu.uniobuda.nik.openday;

import java.util.List;

import hu.ponte.mobile.smartcity.api.SmartCityApi;
import hu.ponte.mobile.smartcity.api.exceptions.DirectionNotFoundException;
import hu.ponte.mobile.smartcity.api.exceptions.OutOfBoundsException;
import hu.ponte.mobile.smartcity.api.exceptions.ServerNotRespondException;
import hu.uniobuda.nik.openday.maps.PositionOverlays;
import hu.uniobuda.nik.openday.utils.Config;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class GettingHereActivity extends BaseActivity {

	MapView map;
	MapController controller;
	GeoPoint defaultCenterPoint = Config.DEFAULT_CENTER_POINT;
	int defaultZoomLevel = Config.DEFAULT_ZOOM_LEVEL;

	LocationManager manager;

	BitmapDrawable pegmanDrawable, nikDrawable;
	PositionOverlays positions, nikPositions;
	List<Overlay> overlays;
	
	SmartCityApi sca;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.getting_here);

		map = (MapView) findViewById(R.id.opendayMap);
		map.setBuiltInZoomControls(true);
		controller = map.getController();
		controller.setCenter(defaultCenterPoint);
		controller.setZoom(defaultZoomLevel);

		if (pegmanDrawable == null)
			pegmanDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.pegman);
		if (nikDrawable == null)
			nikDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.nik);
		pegmanDrawable.setTargetDensity(getResources().getDisplayMetrics().densityDpi);
		nikDrawable.setTargetDensity(getResources().getDisplayMetrics().densityDpi);
		positions = new PositionOverlays(pegmanDrawable);
		nikPositions = new PositionOverlays(nikDrawable);
		nikPositions.setPosition(new OverlayItem(defaultCenterPoint, null, null));		
		overlays = map.getOverlays();
		manager = (LocationManager) getSystemService(LOCATION_SERVICE);
		
		setButtonVisible();
		setOnButtonclick(R.string.plan, new OnClickListener() {
			public void onClick(View v) {
				plan();
			}
		});
	
		initPositions();
	}	

	@Override
	protected void onResume() {
		super.onResume();
		manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Config.LOCATION_MIN_TIME, Config.LOCATION_MIN_DISTANCE, listener);
		manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Config.LOCATION_MIN_TIME, Config.LOCATION_MIN_DISTANCE, listener);
	}

	@Override
	protected void onPause() {
		super.onPause();
		manager.removeUpdates(listener);
	}	
	
	private void removePositions(){
		overlays.remove(nikPositions);
		overlays.remove(positions);
	}
	
	private void initPositions() {	
		removePositions();
		overlays.add(nikPositions);
		overlays.add(positions);		
	}
	
	private void planClear(){
		if(sca != null){
			sca.clearMap();
			sca.interrupt();
			sca = null;
		}
		initPositions();
	}
	
	private void plan(){
		if(positions.getPosition() != null){
			planClear();
			sca = new SmartCityApi(Config.SMARTCITY_API_KEY, map);
			sca.setFrom(positions.getPosition().getPoint());
			sca.setTo(nikPositions.getPosition().getPoint());
			sca.setLessWalk(true);
			sca.setLanguage(SmartCityApi.LANG_HU);
			sca.setResults(new SmartCityApi.PlanningResults() {
				public void onSuccess() {
					setBusy(false);			
					removePositions();
					sca.animateToStartPoint();
					sca.showFirstAnnotation();
				}
				public void onException(Exception e) {
					setBusy(false);
					if (e instanceof ServerNotRespondException)
						Toast.makeText(GettingHereActivity.this, "Szerver nem válaszol!", Toast.LENGTH_LONG).show();
					else if (e instanceof OutOfBoundsException)
						Toast.makeText(GettingHereActivity.this, "Tervezési területen kivül!", Toast.LENGTH_LONG).show();
					else if (e instanceof DirectionNotFoundException)
						Toast.makeText(GettingHereActivity.this, "Nincs útvonal.", Toast.LENGTH_LONG).show();
					else
						Toast.makeText(GettingHereActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
				}
			});
			sca.start();
			setBusy(true);
		}else{
			Toast.makeText(GettingHereActivity.this, getString(R.string.no_position), Toast.LENGTH_LONG).show();					
		}
	}
	
	private LocationListener listener = new LocationListener() {
		public void onStatusChanged(String provider, int status, Bundle extras) {}
		public void onProviderEnabled(String provider) {}
		public void onProviderDisabled(String provider) {}
		public void onLocationChanged(Location location) {
			GeoPoint point = new GeoPoint(
					(int) (location.getLatitude() * 1E6),
					(int) (location.getLongitude() * 1E6));
			OverlayItem item = new OverlayItem(point, null, null);
			positions.setPosition(item);
			controller.animateTo(point);
		}
	};

}
